package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.AttributeMapper;
import cn.tedu.csmall.product.mapper.AttributeTemplateMapper;
import cn.tedu.csmall.product.pojo.dto.AttributeAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.AttributeUpdateDTO;
import cn.tedu.csmall.product.pojo.entity.Attribute;
import cn.tedu.csmall.product.pojo.vo.AttributeListItemVO;
import cn.tedu.csmall.product.pojo.vo.AttributeStandardVO;
import cn.tedu.csmall.product.service.IAttributeService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class AttributeServiceImpl implements IAttributeService {
    @Autowired
    private AttributeMapper attributeMapper;
    @Autowired
    private AttributeTemplateMapper attributeTemplateMapper;

    public AttributeServiceImpl() {
        log.debug("创建业务对象：AttributeService");
    }

    @Override
    public void insert(AttributeAddNewDTO attributeAddNewDTO) {
        log.debug("开始处理【添加属性】的业务，参数：{}", attributeAddNewDTO);
        int count = attributeMapper.countByNameAndTemplateId(attributeAddNewDTO.getName(), attributeAddNewDTO.getTemplateId());
        if (count>0){
            String message = "添加属性失败，尝试添加的属性名称已被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        Object queryResult = attributeTemplateMapper.getStandardById(attributeAddNewDTO.getTemplateId());
        if (queryResult == null){
            String message = "添加属性失败，尝试添加的属性所属的属性模板不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        Attribute attribute = new Attribute();
        BeanUtils.copyProperties(attributeAddNewDTO, attribute);
        log.debug("即将执行插入数据!");
        int rows = attributeMapper.insert(attribute);
        if (rows!=1){
            String message = "添加属性失败，服务器忙，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public void update(Long id, AttributeUpdateDTO attributeUpdateDTO) {
        log.debug("开始处理【修改属性详情】的业务，参数id：{}，新数据：{}", id, attributeUpdateDTO);
        Object queryResult = attributeMapper.getStandardById(id);
        if (queryResult == null){
            String message = "修改属性详情失败，尝试修改的属性不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        int count = attributeMapper.countByNameAndNotId(id, attributeUpdateDTO.getName());
        if (count>0){
            String message = "修改属性失败，尝试修改的属性名称已被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        Attribute attribute = new Attribute();
        BeanUtils.copyProperties(attributeUpdateDTO, attribute);
        attribute.setId(id);
        log.debug("即将执行修改数据");
        int rows = attributeMapper.update(attribute);
        // TODO 待处理
    }

    @Override
    public AttributeStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据id查询属性详情】的业务，参数id：{}", id);
        AttributeStandardVO queryResult = attributeMapper.getStandardById(id);
        if (queryResult == null){
            String message = "根据id查询属性详情失败，尝试查询的属性不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        log.debug("即将返回数据详情：{}", queryResult);
        return queryResult;
    }

    @Override
    public List<AttributeListItemVO> listByTemplateId(Long templateId) {
        log.debug("开始处理【根据属性模板id查询属性列表】的业务，参数id：{}", templateId);
        int count = attributeMapper.countByTemplateId(templateId);
        if (count == 0){
            String message = "根据属性模板id查询属性列表失败，尝试查询的属性模版不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        List<AttributeListItemVO> queryList = attributeMapper.listByTemplateId(templateId);
        log.debug("即将返回数据列表：{}", queryList);
        return queryList;
    }
}
